package com.itzb.asone.admin.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itzb.asone.admin.constant.AdminConstant;
import com.itzb.asone.application.BaseController;
import com.itzb.asone.constant.model.SysOrgConstant;
import com.itzb.asone.constant.model.SysUserConstant;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysGroupUser;
import com.itzb.asone.system.model.SysOrg;
import com.itzb.asone.system.model.SysUser;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysDictService;
import com.itzb.asone.system.service.ISysOrgService;
import com.itzb.asone.utils.GitId;
import com.itzb.asone.utils.JsonResult;
import com.itzb.asone.utils.PageResult;

@Controller
@RequestMapping(value = "sysorg")
public class OrgController extends BaseController {
	
	private Logger logger = Logger.getLogger(getClass()); 
	
	@Autowired
	private IMessageService messageService;
	@Autowired
	private ISysOrgService sysOrgService;
	@Autowired
	private ISysDictService sysDictService;

	/**
	 * 显示机构列表
	 * @param map
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public String list(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		return "system/sysorg/list";
	}

	/**
	 * 获取机构列表
	 * @param page
	 * @param pageSize
	 * @param order
	 * @param sort
	 * @param request
	 * @param response
     * @return
     */
	@ResponseBody
	@RequestMapping(value = "/getdata", method = { RequestMethod.POST })
	public PageResult getData(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int page,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int pageSize,
			@RequestParam(required = false, value = "order") String order,
			@RequestParam(required = false, value = "sort") String sort,
			HttpServletRequest request,HttpServletResponse response
			) {
		PageResult pageResult = new PageResult();	
		Criteria<SysOrg> param = new Criteria<SysOrg>();
		param.setPageSize(pageSize).setStartIndex(page);
		param.setOrderBy(sort + " " + order);
		List<SysOrg> list = sysOrgService.queryPage(param);
		List<Map<String, Object>> listMap = sysDictService.translateToMapList(list);
		pageResult.setTotal(sysOrgService.queryPageCount(param));
		pageResult.setRows(listMap);
		return pageResult;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getjsondata")
	public JSONArray getJsonData(HttpServletRequest request,HttpServletResponse response) {
		
		JSONArray result = new JSONArray();
		JSONObject json = new JSONObject();
		Criteria<SysOrg> param = new Criteria<SysOrg>();
		param.addParam("orgStatus", SysOrgConstant.T_SYS_ORG_STATUS_1);
		List<SysOrg> list = sysOrgService.queryPage(param);
		for(SysOrg sysOrg : list) {
			json = new JSONObject();
			json.put("id", sysOrg.getOrgCode());
			json.put("pId", (sysOrg.getParentOrgCode()==null||sysOrg.getParentOrgCode().equals(""))?"0":sysOrg.getParentOrgCode());
			json.put("name", sysOrg.getOrgName());
			result.add(json);
		}
		return result;
	}

	/**
	 * 显示新增机构页面
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping(value = "/showmodify", method = { RequestMethod.GET })
	public String showModify(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("id");
		SysOrg sysOrg = new SysOrg();
		if(id != null && !id.equals("")){
			sysOrg = sysOrgService.queryEntityById(id);
		}
		modelMap.put("sysOrg", sysOrg);
		return "system/sysorg/modify";
	}

	/**
	 * 新增机构
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public JsonResult modify(SysOrg sysOrg,HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		String showtype = request.getParameter("showtype");
		Criteria<SysOrg> param = new Criteria<SysOrg>();
		if(showtype.equals(AdminConstant.SHOW_TYPE_UPDATE)){
			param.addParam("id", sysOrg.getId());
			param.setRecord(sysOrg);
			sysOrgService.updateByCriteria(param);
		} else {
			sysOrg.setId(GitId.primaryKey());
			sysOrg.setCreateTime(new Date());
			sysOrgService.insert(sysOrg);
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}
	
	/**
	 * 删除机构
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/del", method = { RequestMethod.POST })
	public JsonResult del(HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		SysOrg sysOrg = new SysOrg();
		Criteria<SysOrg> param = new Criteria<SysOrg>();
		String[] ids = request.getParameterValues("id[]");
		for(int i=0;i<ids.length;i++){
			param.clearParams();
			param.addParam("id", ids[i]);
			sysOrg.setOrgStatus(Integer.parseInt(SysOrgConstant.T_SYS_ORG_STATUS_2));
			param.setRecord(sysOrg);
			sysOrgService.updateByCriteria(param);
			
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}

}
